require('../common/header/index')
var _store_service = require('../../service/store-service')
var _product_service = require('../../service/product-service')
var _header_service = require('../../service/header-service')
const vue = require('../common/vue');
const _util = require('../../util/util');

let _detail_content = new vue({
    el: "#mainBody",
    data() {
        return {
            store: {
                shopid: '',
                name: '',
                address: '',
                salesvolume: 5,
                favorablerate: 4.9,
                sellername: '',
                avaiable: 1
            },
            item: {
                productid: '',
                shopid: '',
                price: '',
                stock: '',
                pictureid: '',
                description: '',
                available: 1,
                isnew: 1,
                isrecommend: 0,
                itemattrlist: [],
            },
            productList: [],
            categoryList: [],
            secondCategoryList: [],
            selectedCategory: 1,
            selectedProduct: 1,
            name: '',
            address: '',
            sellername: '',
            pageid: 1,
            info: ''
        }
    },
    methods: {
        reset: function () {
            this.store.name = this.name;
            this.store.address = this.address;
            this.store.sellername = this.sellername;
        },
        updateStore: function () {
            let self = this;
            _store_service.updateStore(
                this.store,
                function (res) {
                    alert('修改成功！')
                    window.location.href = '/view/mystore_detail.html?shopid=' + self.store.shopid;
                },
                function (errMsg) {
                    console.log(errMsg)
                }
            )
        },
        updateList: function (event) {
            _mystore_detail.getAllItems();
        },
        backpage: function (event) {
            console.log(this.info.pages)
            if (this.pageid > 1) {
                this.pageid = this.pageid - 1;
                this.updateList();
            }
        },
        nextpage: function (event) {
            console.log(this.info.pages)
            console.log(this.pageid * 1 + 1 <= this.info.pages * 1)
            if (this.pageid * 1 + 1 <= this.info.pages * 1) {
                console.log("进去了")
                this.pageid = this.pageid * 1 + 1;
                this.updateList();
            }
        },
        load2ndCategory: function (id) {
            let self = this
            _product_service.getProducts(
                id,
                function (res) {
                    self.secondCategoryList = res;
                    self.selectedProduct = res[0].productid;
                },
                function (errMsg) {
                    console.log(errMsg)
                }
            )
        },
        addItem: function () {
            let self = this;
            this.item.productid = this.selectedProduct;
            this.item.shopid = this.store.shopid;
            let itemVO2 = this.item
            _store_service.addNewItem(
                itemVO2,
                function (res) {
                    alert('添加成功！');
                    window.location.href = '/view/mystore_detail.html?shopid=' + self.store.shopid;
                },
                function (errMsg) {
                    console.log(errMsg)
                }
            )
        },
        updateItem: function (product) {
            let self = this;
            _product_service.updateItem(
                product.id,
                product.stock,
                product.price,
                product.description,
                function (res) {
                    alert('修改成功！')
                    window.location.href = '/view/mystore_detail.html?shopid=' + self.store.shopid;
                },
                function (errMsg) {
                    console.log(errMsg);
                }
            )
        },
        deleteItem: function (product, index) {
            let self = this;
            _product_service.manageItem(
                product.id,
                0,
                function (res) {
                    alert('下架成功！')
                    self.productList[index].available = 0;
                },
                function (errMsg) {
                    console.log(errMsg);
                }
            )
        },
        grounding: function (product, index) {
            let self = this;
            _product_service.manageItem(
                product.id,
                1,
                function (res) {
                    alert('上架成功！')
                    self.productList[index].available = 1;
                },
                function (errMsg) {
                    console.log(errMsg);
                }
            )
        }
    },
})
var _mystore_detail = {
    _this: this,
    init: function () {
        this.bindEvents();
        this.getOneStore();
        this.getAllItems();
        this.loadCategory();
        this.load2ndCategory();
    },
    bindEvents: function () {

    },
    getOneStore: function () {
        let shopid = decodeURI(_util.getQueryVariable('shopid'));
        _store_service.getOneStore(
            shopid,
            function (res) {
                _detail_content.store = res;
                _detail_content.name = res.name;
                _detail_content.address = res.address;
                _detail_content.sellername = res.sellername;
            },
            function (errMsg) {
                console.log(errMsg);
            }
        )
    },
    getAllItems: function () {
        let shopid = decodeURI(_util.getQueryVariable('shopid'));
        _product_service.getItemsPage(
            shopid,
            6,
            _detail_content.pageid,
            0,
            function (res) {
                _detail_content.productList = res.records;
                for (let i = 0; i < _detail_content.productList.length; i++) {
                    var pair = _detail_content.productList[i].src.split("\"");
                    _detail_content.productList[i].src = "../image/" + pair[1].substring(7);
                }
                _detail_content.info = res;
            },
            function (errMsg) {
                console.log(errMsg);
            }
        )
    },
    loadCategory: function () {
        _header_service.getAllCategories(
            function (res) {
                _detail_content.categoryList = res;
                // _detail_content.selectedCategory = '--选择--';
            },
            function (errMsg) {
                console.log("failed!")
            },
        )
    },
    load2ndCategory: function () {
        _detail_content.load2ndCategory(1);
    }
}
module.exports = _mystore_detail.init();